# -*- mode: python; -*-
import os
import subprocess
import sys

vpath = '#/third_party/thrift-0.8.0'

env = DefaultEnvironment()

config_opts = '--prefix=' + str(Dir('#/build')) + ' ' + '--disable-dependency-tracking ' + '--with-boost=' + str(Dir('#/build')) + ' ' + '--without-csharp --without-java --without-erlang --without-python ' + '--without-perl --without-php --without-ruby --without-haskell  --without-go --without-tests'

if not sys.platform.startswith('freebsd'):
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; make clean; make; make install)')
else:
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; gmake clean; gmake; gmake install)')

version = '0.8.0'
libs = [ str(File('#build/lib/libthrift.a')),
         str(File('#build/lib/libthriftasio.a')) ]
shlibs = [ str(File('#build/lib/libthrift-' + version + '.so')),
           str(File('#build/lib/libthriftasio.so.0.0.0')) ]


products = [ '#build/bin/thrift',
             '#build/include/thrift/Thrift.h',
             '#build/include/thrift/TLogging.h',
             '#build/include/thrift/config.h' ]
products += libs + shlibs

thrift_cfg = env.Command(products, str(Dir(vpath)), cmd)

env.Requires(thrift_cfg, '#/build/include/boost')

env.Alias('install', env.Install(env['INSTALL_LIB'], shlibs))
env.Alias('install',
          env.Symlink(env['INSTALL_LIB'] + '/libthriftasio.so' + '.0',
                      env['INSTALL_LIB'] + '/libthriftasio.so' + '.0.0.0'))

async_files = ['TAsioAsync.h', 'TAsync.h', 'TFuture.h']
for file in async_files:
    env.Install('#build/include/thrift/async',
                vpath + '/lib/cpp/src/async/' + file)
